package com.myth.date;

import org.junit.Test;

import java.time.*;
import java.util.Date;

/**
 * jdk 8中日期时间AP1的测试
 *
 *
 * @author
 * @create 2021/8/12
 */
public class JDK8DateTimeTest {

    @Test
    public void testDate(){
        Date date1 = new Date(2020, 05, 20);
        System.out.println(date1);
    }

    /**
     * LocalDate. LocalTime. LocalDateTime 的使用
     * 说明:
     *      1.LocalDateTime相较于LocalDate、LocalTime，使用频率要高
     */

    @Test
    public void test1(){
        // now()∶获取当前的日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():
        LocalDateTime localDateTime1 = LocalDateTime.of(2020,05,20,13,14);
        System.out.println(localDateTime1);

        //getXxx()
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());//英文
        System.out.println(localDateTime.getMonth());//英文
        System.out.println(localDateTime.getMonthValue());//数字
        System.out.println(localDateTime.getMinute());

        //体现了不可变性
        // withXxx(()∶设置相关的属性
        System.out.println(localDateTime);
        System.out.println(localDateTime.withDayOfMonth(22));

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println( "localDateTime:"+localDateTime) ;
        System.out.println( "localDateTime2:"+localDateTime2);

        //体现了不可变性+
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3) ;
        System.out.println( localDateTime) ;
        System.out.println( localDateTime3);
        //体现了不可变性-
        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println( localDateTime) ;
        System.out.println( localDateTime4);
    }

    /**
     * Instant的使用
     *
     */


    @Test
    public void test2(){
        //now():获取本初子午线对应的标准时间
        Instant instant= Instant.now( );
        System.out.println(instant);//2021-08-12T05:19:32.822Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime) ;

        //获取自1970年1月1日e时e分e秒(UTC）开始的毫秒数 ---> Date类的getTime()
        long milli = instant.toEpochMilli();
        System.out.println(milli);

        //ofEpochMilli():通过给定的毫秒数，获取Instant实例-->Date(Long millis)
        Instant instant1 = Instant.ofEpochMilli(1550475314878L);
        System.out.println(instant1);


    }



}
